package com.sx.zongzhi.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.sx.zongzhi.BaseActivity;
import com.sx.zongzhi.R;
import com.sx.zongzhi.bean.KeyAreaBean;
import com.sx.zongzhi.bean.KeyAreaDetailBean;
import com.sx.zongzhi.bean.MsgEvent;
import com.sx.zongzhi.net.ServiceFactory;
import com.sx.zongzhi.presenter.KeyAreaDetailPest;
import com.sx.zongzhi.utils.BaseUtil;
import com.sx.zongzhi.utils.MyRxBus;
import com.sx.zongzhi.utils.ToastUtils;
import com.sx.zongzhi.view.IKeyAreaDetailView;
import com.sx.zongzhi.widget.CustomDialog;
import com.sx.zongzhi.widget.LoadDialog;

import java.io.File;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * 重点区域详情
 */
public class KeyAreaDetailActivity extends BaseActivity implements IKeyAreaDetailView {


    @BindView(R.id.imv_Head) ImageView imv_Head;
    @BindView(R.id.tv_KeyAreaName) TextView tv_KeyAreaName;
    @BindView(R.id.tv_KeyAreaType) TextView tv_KeyAreaType;
    @BindView(R.id.tv_AreaAddress) TextView tv_AreaAddress;
    @BindView(R.id.tv_Police) TextView tv_Police;
    @BindView(R.id.tv_Header) TextView tv_Header;
    @BindView(R.id.tv_Telephone) TextView tv_Telephone;
    @BindView(R.id.tv_Phone) TextView tv_Phone;
    @BindView(R.id.tv_Attention) TextView tv_Attention;
    @BindView(R.id.tv_Danger) TextView tv_Danger;
    @BindView(R.id.tv_DangerState) TextView tv_DangerState;
    @BindView(R.id.ll_DangerState) LinearLayout ll_DangerState;
    @BindView(R.id.tv_DangerChange) TextView tv_DangerChange;
    @BindView(R.id.ll_DangerChange) LinearLayout ll_DangerChange;
    @BindView(R.id.tv_Remark) TextView tv_Remark;
    /**重点区域id*/
    private String keyAreaId="";
    private String orgId="";
    private KeyAreaDetailPest keyAreaDetailPest;
    private KeyAreaDetailBean detailBean;
    @Override
    public void setUI(Bundle savedInstanceState) {
        setContentView(R.layout.activity_key_area_detail);
        ButterKnife.bind(this);
        setTitleName(R.string.key_area_detail);
        keyAreaId=getIntent().getStringExtra("keyAreaId");
        orgId = getIntent().getStringExtra("orgId");
        initViews();
        initData();
    }

    private void initViews() {
        KeyAreaBean.PermissionBean userPermission = ( KeyAreaBean.PermissionBean)
                getIntent().getSerializableExtra("userPermission");
        if (userPermission.isDeleteImportantArea()){
            setRightImage(R.mipmap.icon_nav_delete);
        }
        if (userPermission.isUpdateImportantArea()){
            setRightImage2(R.mipmap.icon_nav_edit);
        }
    }

    private void initData() {
        keyAreaDetailPest=new KeyAreaDetailPest(this);
        LoadDialog.show(this);
        keyAreaDetailPest.getKeyAreaList(keyAreaId);
    }



    @OnClick({R.id.imv_Right,R.id.imv_Right2,R.id.imv_Head})
    public void OnClick(View view){
        switch (view.getId()){
            case R.id.imv_Head://
                if (detailBean!=null&& !BaseUtil.isEmpty(detailBean.getImgUrl())){
                    String path=ServiceFactory.NEW_API_BASE_URL+File.separator+detailBean.getImgUrl();
                    startActivity(new Intent(this, ImagePlayActivity.class).putExtra(
                            "path", path));
                }
                break;
            case R.id.imv_Right://删除
                createDelete();
                break;
            case R.id.imv_Right2://编辑
                if (detailBean!=null){
                    startActivity(new Intent(this,KeyAreaEditActivity.class)
                            .putExtra("keyAreaId",keyAreaId)
                            .putExtra("KeyAreaDetailBean",detailBean)
                            .putExtra("orgId",orgId));
                    finish();
                }else {
                    ToastUtils.showToast(this,"未获取到详情 编辑失败");
                }
                break;
        }
    }

    private void createDelete() {
        CustomDialog.Builder builder=new CustomDialog.Builder(this);
        CustomDialog dialog=builder.setMessage("确定删除该区域").
                setBackButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                }).setConfirmButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                LoadDialog.show(KeyAreaDetailActivity.this,"删除中");
                keyAreaDetailPest.KeyAreaDelete(keyAreaId);
                dialog.dismiss();
            }
        }).create();
        dialog.show();
    }


    @Override
    public void showError(String message) {
        LoadDialog.dismiss(this);
        ToastUtils.showToast(this,message);
    }

    @Override
    public void getKeyAreaDetail(KeyAreaDetailBean detailBean) {
        LoadDialog.dismiss(this);
        if (detailBean!=null){
            this.detailBean=detailBean;
            tv_KeyAreaName.setText(detailBean.getName());
            tv_KeyAreaType.setText(detailBean.getType()!=null ?
                    detailBean.getType().getDisplayName():"未知");
            tv_AreaAddress.setText(detailBean.getAddress());
            tv_Police.setText(detailBean.getNativePolice());
            tv_Header.setText(detailBean.getLeader());
            tv_Telephone.setText(detailBean.getTelephone());
            tv_Phone.setText(detailBean.getMobile());
            tv_Attention.setText(detailBean.getAttentionExtent()!=null ?
            detailBean.getAttentionExtent().getDisplayName():"未知");
            if (detailBean.isHasDanger()){
                tv_Danger.setText("有");
                tv_DangerChange.setText(detailBean.getTreatDanger());
                tv_DangerState.setText(detailBean.getDangerDes());
            }else {
                tv_Danger.setText("无");
                ll_DangerChange.setVisibility(View.GONE);
                ll_DangerState.setVisibility(View.GONE);
            }
            tv_Remark.setText(detailBean.getRemark());
            if (!BaseUtil.isEmpty(detailBean.getImgUrl())) {
                final String path = ServiceFactory.NEW_API_BASE_URL + File.separator + detailBean.getImgUrl();
                RequestOptions options = new RequestOptions()
                        .centerCrop()
                        .placeholder(R.mipmap.img_enterprise)
                        .error(R.mipmap.img_enterprise)
                        .diskCacheStrategy(DiskCacheStrategy.RESOURCE);
                Glide.with(this)
                        .load(path)
                        .apply(options)
                        .into(imv_Head);
                imv_Head.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        startActivity(new Intent(KeyAreaDetailActivity.this,ImagePlayActivity.class)
                        .putExtra("path",path));
                    }
                });
            }
        }
    }

    @Override
    public void KeyAreaDelete(String str) {
        LoadDialog.dismiss(this);
        ToastUtils.showToast(this,str);
        MyRxBus.get().post(new MsgEvent("KeyAreaActivity",1,str));
        finish();
    }
}
